import {isNumber} from 'lodash'
import utils from '@/utils/index'
import { ASSETS_HOST } from '@/config'

// 上传
export const upLoadFile = async (file, dPath) => {
    const hash = await utils.file.md5(file.url)
    console.log('jp======hash',hash)
    const fileName = `${dPath}/${hash}${utils.file.ext(file.url)}`;
    console.log('jp======fileName',fileName)
    const sign:any = utils.oss(fileName)
    return sign
}
export const uniUpload = (sign: any, file: any) => {
    return new Promise((resolve, reject) =>{
        uni.uploadFile({
            url: sign.host,
            filePath: file.url,
            name: 'file',
            formData: sign,
            success: () => {
                console.log('jp======上传成功url', `${ASSETS_HOST}/${sign.key}`)
                const __newUrl = `${ASSETS_HOST}/${sign.key}`
                resolve(__newUrl)
            },
            fail: () => {
                reject()
            }
        });
    })
}

const handleDeep = (dataArr, value) => {
    let valueData = ''
    for (const i in dataArr) {
        const item = dataArr[i]
        if (item.value === value || item.id === value) {
            valueData = item
            break
        }
        if (item.children && item.children.length > 0) {
            valueData = handleDeep(item.children, value)
            if (valueData) break
        }
    }
    return valueData
}
export const matchDictionaryName = (dataArr, value) => {
    let valueData
    if (value || value === 0) {
        const __value = isNumber(value) ? String(value) : value
        for (const i in dataArr) {
            const item = dataArr[i]
            const id = item.value || item.id
            if (id || String(id) === __value) {
                valueData = item
                break
            }
            if (item.children && item.children.length > 0) {
                valueData = handleDeep(item.children, __value)
                if (valueData) break
            }
        }
    }
    if (valueData) {
        return valueData.name || valueData.label
    }
    return '--'
}

// 校验数字
export const validateNumber = (value) => {
    const reg = new RegExp(/^[0-9]*$/)
    console.log('jp======手机号',value)
    return reg.test(value)
}

// 校验邮箱
export const validateEmail = (value) => {
    console.log('jp======value====邮箱',value)
    const reg =
        /^(([^<>()\]\\.,;:\s@"]+(\.[^<>()\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return reg.test(value)
}
//  校验手机号
export const validatePhone = (value) => {
    const reg = /^1[0-9]{10}$/
    return reg.test(value)
}
//  校验手机号
export const validateMobile = (value) => {
    const reg = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/
    return reg.test(value)
}
//  中文校验
export const validateChinese = (value) => {
    const reg = /^[\u4E00-\u9FA5]+$/
    return reg.test(value)
}